package com.ahui.mallchat.common.common.algorithm.ac;

import com.google.common.collect.Maps;
import lombok.Getter;
import lombok.Setter;

import java.util.Map;

/**
 * Author:ahui
 * Description:
 * Date: 2024/8/6 14:08
 */
@Getter
@Setter
public class ACTrieNode {

    // 子节点
    private Map<Character, ACTrieNode> children = Maps.newHashMap();

    //匹配过程中，如果模式串不匹配，模式串指针会回退到failover继续进行匹配
    private ACTrieNode failover = null;

    private int depth;

    private boolean isLeaf = false;

    public void addChildrenIfAbsent(char aChar) {
        children.computeIfAbsent(aChar,(key) -> new ACTrieNode());
    }

    public ACTrieNode childOf(char aChar) {
        return children.get(aChar);
    }

    public boolean hasChild(Character key) {
        return children.containsKey(key);
    }

    @Override
    public String toString() {
        return "ACTrieNode{" +
                "children=" + children +
                ", failover=" + failover +
                ", depth=" + depth +
                ", isLeaf=" + isLeaf +
                '}';
    }
}
